﻿


var actionUrl = '/bidder/ashx/bidderhandler.ashx';
var formUrl = '/bidder/html/bidder.html';
var actiondll = '/tenderinfo/ashx/baghandler.ashx';
var formUrlz = '/bidder/html/upload.html';
var uploadCompareprice = '/bidder/html/uploadPriceCompare.html';
var ids = "";
var filterObj = "";
$(function () {
    
    $('#bid').combobox({
        multiple: false, valueField: 'KeyId', textField: 'bagname', panelHeight: 'auto', onLoadSuccess: function () {
            var data = $('#bid').combobox('getData');
            if (data.length > 0)
                $("#bid").combobox('select', data[0].KeyId);
            getopt();
            autoResize({ dataGrid: '#mGrid', gridType: 'datagrid', callback: mygrid.bindGrid, height: 5 });
        }
    });
    $.getJSON(actiondll + '?' + crud.createParam('dll'), function (d) {
        $('#bid').combobox('loadData', d);
    });   
    $('#setbg').click(crud.setbag);
    $('#audit').click(crud.audit);
    $('#ftxt').click(crud.ftxt);
    
});

var mygrid = {
    bindGrid: function (size) {
        $('#mGrid').datagrid({
            toolbar: '#toolbar',
            title: '包投标商列表',
            iconCls: 'icon icon-list',
            width: size.width,
            height: size.height,
            nowrap: false,
            rownumbers: true,
            animate: true,
            url: actionUrl,
            queryParams: {
                action: 'queryall',
                filter: JSON.stringify(filterObj)
            },
            columns: [[                
				//{ title: '投标商', field: 'KeyId', width: 100 },
				{ title: '名称', field: 'bname', width: 150 },
                { title: '投标商编号', field: 'bbh', width: 100 },
                {
                    title: '上传方案', field: 'u', width: 100, formatter: function (v, d, i) {

                        return '<a href="#" style="color:blue" onclick="crud.addz(' + d.KeyId + ',\''+d.bagbh+d.bagid+'\');">上传方案</a>';

                    }
                },
                {
                    title: '上传材料用量明细', field: 'addexcel', width: 150, formatter: function (v, d, i) {

                        return '<a href="#" style="color:blue" onclick="crud.addexcel(' + d.KeyId + ',\'' + d.bagid + '\');">上传材料用量明细</a>';

                    }
                },
				{ title: '手机', field: 'bphone', width: 100 },
				{ title: '股东名单', field: 'bshareholders', width: 180 },				
				{ title: '固定电话', field: 'btel', width: 100 },				
				{ title: '邮箱', field: 'bemail', width: 100 },
				{ title: '代理人编号', field: 'abh', width: 100 },
                { title: '代理人', field: 'aname', width: 100 },
                { title: '项目经理编号', field: 'mbh', width: 100 },
                { title: '项目经理', field: 'mname', width: 100 },
                { title: '包编号', field: 'bagbh', width: 100 },
                { title: '包名', field: 'bagname', width: 100 }
               

            ]]
        });
    },
    reload: function () {     
        
        $('#mGrid').datagrid('clearSelections').datagrid({ queryParams: { action: 'queryall', filter: JSON.stringify(filterObj) } });
    },
    selectRow: function () {
        return $('#mGrid').datagrid('getSelected');
    },
    getids: function () {
        var rows = $('#mGrid').datagrid('getSelections');
        if (rows == null || rows.length == 0) {
            $.messager.alert("提示", "请选择要设置的行！", "info");
            return;
        }
        ids = "";
        for (var i = 0; i < rows.length; i++)
            ids = ids + rows[i].KeyId + ",";
        if (ids != "")
            ids = ids.substr(0, ids.length - 1);
    }
}

var crud = {
    createParam: function (action, keyid) {
        var o = {};
        var query = top.$('#myForm').serializeArray();
        query = convertArray(query);
        o.jsonEntity = JSON.stringify(query);
        o.action = action;
        o.keyid = keyid;
        return "json=" + JSON.stringify(o);
    },
    addz: function (id,bagbh) {
        var addDialog = top.$.hDialog({
            href: formUrlz + '?v=' + Math.random(), height: 400, width: 500, title: "增加", iconCls: 'icon-add',
            onLoad: function () {

            },
            submit: function () {
                if (top.$('#myForm').validate().form()) {
                    //var query = crud.createParam('up', id);
                    top.$('#myForm').form('submit', {
                        url: actionUrl+"?action=up&id="+id+"&bagbh=" + bagbh,
                        onSubmit: function () {
                            return $(this).form('validate');
                        },
                        success: function (result) {
                            addDialog.dialog('close');
                            msg.ok(result);
                        }
                    });
                    
                }
                return false;
            }
        });
    },
    addexcel: function (id, bagid) {
        var addDialog = top.$.hDialog({
            href: uploadCompareprice + '?v=' + Math.random(), height: 400, width: 500, title: "增加", iconCls: 'icon-add',
            onLoad: function () {

            },
            submit: function () {
                if (top.$('#myForm').validate().form()) {
                    //var query = crud.createParam('up', id);
                    top.$('#myForm').form('submit', {
                        url: actionUrl + "?action=upexcel&id=" + id + "&bagid=" + bagid,
                        onSubmit: function () {
                            return $(this).form('validate');
                        },
                        success: function (result) {
                            addDialog.dialog('close');
                            msg.ok(result);
                        }
                    });

                }
                return false;
            }
        });
    },
    audit: function () {
        var bagid = $("#bid").combobox('getValue');
        $.ajaxjson(actionUrl, "action=audit&bagid=" + bagid, function (d) {
            if (d.Success) {
                var addDialog = top.$.hDialog({
                    content: d.Message + "<br /><br /><br /><a href='" + d.Data + "' style='color:blue'>文档下载</a>", height: 500, width: 600, title: "结果", submit: function () {
                        addDialog.dialog('close');
                    }
                });               
            } else {
                MessageOrRedirect(d);
            }
           
        });
    },
    ftxt: function () {
        var bagid = $("#bid").combobox('getValue');
        var ft = $("#ftext").val();
        $.ajaxjson(actionUrl, "action=ftxt&bagid=" + bagid+"&ftxt="+ft, function (d) {
            if (d.Success) {
                var addDialog = top.$.hDialog({
                    content: d.Message, height: 400, width: 500, title: "结果", submit: function () {
                        addDialog.dialog('close');
                    }
                });
            } else {
                MessageOrRedirect(d);
            }

        });
    },
    setbag: function () {        
        getopt();
        mygrid.reload();
    }
}
function getopt() {
    var bagid = $("#bid").combobox('getValue');
    var ruleArr = [];
    
    ruleArr.push({ "field": "bagid", "op": "eq", "data": bagid });
   

    if (ruleArr.length > 0)
        filterObj = { groupOp: 'AND', rules: ruleArr };
    else
        filterObj = "";

}